package jpa.beans;

import java.io.Serializable;
import java.util.Collection;
import javax.ejb.Remote;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import jpa.entities.Bestelling;
import jpa.entities.BestellingPK;
import jpa.entities.Klant;
import jpa.entities.Product;
import jpa.interfaces.KlantBeanInterface;

@Remote(KlantBeanInterface.class)
@Stateful(mappedName = "KlantBeans")
public class KlantBean implements Serializable, KlantBeanInterface {
	private static final long serialVersionUID = -2875458375584064192L;

	@PersistenceContext
	EntityManager em;

	protected Collection<Klant> alleKlanten;

	@Override
	public void addKlant(String voornaam, String achternaam, String adres,
			String post, String gebruik, String tele, String woon,
			String opmerking) {
		System.out.println("Nieuwe klant");

		Klant klant = new Klant(voornaam, achternaam, adres, post, gebruik,
				tele, woon, opmerking);
		em.persist(klant);

	}

	@Override
	public Collection<Klant> getAllKlants() {

		Query q = em.createQuery("select d from Klant d where d.id > :deptid",
				Klant.class);
		// Query q = em.createQuery("SELECT k FROM Klant AS k");
		q.setParameter("deptid", 1);
		Collection<Klant> departments = q.getResultList();

		System.out.println(departments.size());

		return departments;
	}

	@Override
	public boolean login(String username) {
		Query q = em
				.createQuery(
						"select d from Klant d where d.klant_gebruikersnaam = :deptuser",
						Klant.class);
		q.setParameter("deptuser", username);

		if (q.getResultList().size() == 1) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Klant getKlant(String username) {
		System.out.println("komt binnen: " + username);
		Query q = em.createQuery("select d from Klant d where d.klant_gebruikersnaam = :deptuser");
		q.setParameter("deptuser", username);
		System.out.println(q.getResultList().size());

		return (Klant) q.getSingleResult();

	}

	@Override
	public int getKlantId(String username) {
		
		Query q = em.createQuery("select d from Klant d where d.klant_gebruikersnaam = :deptuser");
		q.setParameter("deptuser", username);
		Klant k = (Klant) q.getSingleResult();

		return  (int) k.getId();

	}

	@Override
	public String getBestelgeschiedenis(int klantID) {
		Query q = em.createQuery("select d from Bestelling d where d.id.klantId= :deptuser",BestellingPK.class);
		q.setParameter("deptuser", klantID);
		String bestelTable = "";
		if(q.getResultList().size()>0)
		{
			bestelTable+="<table>";
			bestelTable+="<tr><td>Bestelid</td><td>Productnaam</td><td>Aantal</td></tr>";
			for(Object o : q.getResultList())
			{
				Bestelling b = (Bestelling) o;
				Query productNaam = em.createQuery("select d.productNaam from Product d where d.id= :deptuser",Product.class);
				productNaam.setParameter("deptuser", b.getId().getProductId());
				String prod_naam = (String)productNaam.getSingleResult();
				bestelTable+="<tr><td>" +b.getId().getBestId()+ "</td><td>"+ prod_naam +"</td><td>"+ b.getId().getAantal() +"</td></tr>";				
			}
			bestelTable+="</table>";
			return bestelTable;
		}
		else
		{
			return "U heeft nog geen bestellingen geplaats.";
		}


	}

	@Override
	public String regKlant(Klant k) {
		// TODO Auto-generated method stub
		if(!login(k.getKlant_gebruikersnaam()))
		{
			em.persist(k);
			return "Succesfull geregistreerd";
		}
		else
		{
			return "fout";
		}
	}

}
